package com.smile.stream;

import com.smile.stream.pojo.Dish;

import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.Optional;
import static java.util.stream.Collectors.*;



/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 20:58
 */
public class CollectDemo0 {
    public static void main(String[] args) {


        //region 最大值
        Comparator<Dish> dishCaloriesComparator = Comparator.comparingInt(Dish::getCalories);
        Optional<Dish> mostCalorieDish = StreamUtil.getMenu()
                .stream()
                .collect(maxBy(dishCaloriesComparator));
        System.out.println(mostCalorieDish);
        //endregion

        //region 最小值
        Optional<Dish> minCalorieDish = StreamUtil.getMenu()
                .stream()
                .collect(minBy(dishCaloriesComparator));
        System.out.println(minCalorieDish);
        //endregion

        //region 汇总 计算所有的卡路里
        int totalCalories = StreamUtil
                .getMenu()
                .stream()
                .collect(summingInt(Dish::getCalories));
        System.out.println("totalCalories = " + totalCalories);
        //endregion

        //region 平均值
        double avgCalories =StreamUtil
                .getMenu()
                .stream()
                .collect(averagingInt(Dish::getCalories));
        System.out.println("avgCalories = " + avgCalories);
        //endregion


        //region 汇总
        IntSummaryStatistics menuStatistics =StreamUtil
                .getMenu()
                .stream()
                .collect(summarizingInt(Dish::getCalories));
        System.out.println("menuStatistics = " + menuStatistics);
        //endregion

        //region 拼接字符串
        String shortMenu = StreamUtil.getMenu().stream().map(Dish::getName).collect(joining());

        System.out.println(shortMenu);
        String shortMenu2 = StreamUtil.getMenu().stream().map(Dish::getName).collect(joining(", "));
        System.out.println(shortMenu2);

        //endregion


        Optional<Dish> mostCalorieDish2 = StreamUtil.getMenu()
                        .stream()
                        .collect(reducing(
                        (d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2));
    }


}
